#include "Bullet.h"


Bullet::Bullet(IDImage gun, float timetolive)
{
	speed = 0.0f;

	m_Sprite = Source::getInstance()->getSprite(gun);
	m_TimeLive = timetolive;

	m_Gametime = new GameTime();
}

void Bullet::setPosition(int x, int y, ShootDirection direction, Auto aut, float angle, float speed)
{
	//speed = 0 => de ko lam lech toa sung
	m_ConstSpeed = speed;
	this->speed = 0.0f;

	shootDirection = direction;
	m_Math.setTam(x, y);
	m_angle = angle;

	if (direction == ShootDirection::_0_SHOOT || direction == ShootDirection::_315_SHOOT
		|| direction == ShootDirection::_45_SHOOT || direction == ShootDirection::_30_SHOOT || direction == ShootDirection::_60_SHOOT
		|| direction == ShootDirection::_300_SHOOT || direction == ShootDirection::_330_SHOOT)
	{
		if (aut == Auto::INCREASE)
			aut = Auto::INCREASE_X;
		else if (aut == Auto::DECREASE)
			aut = Auto::DECREASE_X;
	}
	else
	{
		if (aut == Auto::INCREASE)
			aut = Auto::INCREASE_Y;
		else if (aut == Auto::DECREASE)
			aut = Auto::DECREASE_Y;
	}

	m_Auto = aut;
	m_Position.x = x;
	m_Position.y = y;
}

float Bullet::getAngle()
{
	switch (shootDirection)
	{
	case ShootDirection::_0_SHOOT:
		return 0.0f;
	case ShootDirection::_30_SHOOT:
		return PI / 6;
	case ShootDirection::_45_SHOOT:
		return PI / 4;
	case ShootDirection::_60_SHOOT:
		return PI / 3;
	case ShootDirection::_90_SHOOT:
		return PI / 2;
	case ShootDirection::_270_SHOOT:
		return -PI / 2;
	case ShootDirection::_300_SHOOT:
		return -PI / 3;
	case ShootDirection::_315_SHOOT:
		return -PI / 4;
	case ShootDirection::_330_SHOOT:
		return -PI / 6;
	}
}

void Bullet::Update()
{
	// truong hop ban len cao se tang theo oy
	if (getAngle() == (float)PI / 2 || getAngle() == -(float)PI / 2)
	{
		if (m_Auto == Auto::INCREASE_Y) m_Position.y = IncreaseY();
		else if (m_Auto == Auto::DECREASE_Y)
			m_Position.y = DecreaseY();

		m_Position.x = m_Math.LineWithAngle_Axis_Y(m_Position.y, m_angle);
	}
	else if (m_angle + getAngle() == 0.0f)// se tang toa do auto dan theo truc ox
	{
		if (m_Auto == Auto::INCREASE_X)
			m_Position.x += speed;
		else if (m_Auto == Auto::DECREASE_X)
			m_Position.x -= speed;
	}
	else // tang theo truc ox
	{
		if (m_Auto == Auto::INCREASE_X) m_Position.x = IncreaseX();
		else if (m_Auto == Auto::DECREASE_X) m_Position.x = DecreaseX();

		m_Position.y = m_Math.LineWithAngle_Axis_X(m_Position.x, m_angle + getAngle());
	}

	speed = m_ConstSpeed;
}


void Bullet::setGun(IDImage gun)
{
	m_Sprite = Source::getInstance()->getSprite(gun);
}

bool Bullet::isAlive()
{
	m_Gametime->setTime();

	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeLive)
		return false;

	return true;
}

void Bullet::InitTime()
{
	m_Gametime->setTime();
	m_Gametime->Update();
}

void Bullet::Draw(LPD3DXSPRITE SpriteHandle, float vpx)
{
	m_Sprite->Draw(SpriteHandle, m_Position, D3DXVECTOR2(0.85f, 0.85f), vpx, VIEW_PORT_Y);
}

Bullet::~Bullet()
{
	delete m_Gametime;
}
